﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microhard.Permission.MVCFilter;
using Microhard.WeixinMP.Unities;
using Microhard.WeixinMP.Interfaces;
using Microhard.Core;

namespace Microhard.FMCG.WebUI.Areas.WX.Controllers
{
    public class WeixinMenuController : Controller
    {
        [MVCFilterAtrForLogon]//登录验证
        [MVCFilterAtrForResourcePermission()]//功能权限验证
        public ActionResult Index()
        {
            //获得所有注册的平台信息
            IWXMPAccount mp = IoC.Resolve<IWXMPAccount>();
            var all = mp.Query(null);
            return View(all);
        }

        [MVCFilterAtrForLogon]//登录验证
        [MVCFilterAtrForResourcePermission()]//功能权限验证
        public ActionResult Create()
        {
            //获得所有注册的平台信息
            IWXMPAccount mp = IoC.Resolve<IWXMPAccount>();
            var all = mp.Query(null);
            ViewData["AllMP"] = all;
            return View();
        }

        [MVCFilterAtrForLogon]//登录验证
        [MVCFilterAtrForResourcePermission(OperationKeyName = "Create")]//功能权限验证
        public ActionResult SaveCreate(Guid wxmpid, string MenuJson)
        {
            if (string.IsNullOrEmpty(MenuJson))
            {
                this.ModelState.AddModelError("MenuJson", "请填写菜单内容");
            }
            else
            {
                var error = WeixinTools.CreateMenu(wxmpid, MenuJson);
                if (error != null && error.IsSucceed == false)
                {
                    this.ModelState.AddModelError("MenuJson", error.errmsg);
                    return View("Create");
                }
                return RedirectToAction("Index");
            }
            this.ModelState.AddModelError("MenuJson", "保存失败，请检查json格式是否正确");
            return View("Create");
        }

    }
}
